package chef.myapplication.model;

import chef.myapplication.Main;
import chef.myapplication.controller.MainController;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


import java.io.File;
import java.io.IOException;

public class ImageBox extends VBox {
    private final File file;
    private final CheckBox checkBox = new CheckBox("");
    private final FlowPane flowPane;
    private final ImageBox imageBox;
    private  boolean single;
    private final MainController mainController= MainController.getMainController() ;
    public ImageBox(File file) {
        flowPane= mainController.getFlowPane();
        setAlignment(Pos.CENTER);
        this.file = file;
        setImage();
        initEvent();
        initCheckBox();
        imageBox = this;
        setPadding(new Insets(20.0));
        setOnContextMenuRequested(event -> {
            initContextMenu();
            ImageBoxes.contextMenu.show(flowPane, event.getScreenX(), event.getScreenY());
            event.consume();
            MainController.getMainController().getContextMenu().hide();
        });
    }

    private void initCheckBox() {
        checkBox.setVisible(false);
        checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> {
            if(checkBox.isSelected()){
                ImageBoxes.getSelectedImageBoxes().add(imageBox);
                imageBox.setStyle("-fx-background-color: #6495ED39");
                mainController.setLabel2();
            }
            else {
                ImageBoxes.getSelectedImageBoxes().remove(imageBox);
                imageBox.setStyle("-fx-background-color: \t#00BFFF00");
                mainController.setLabel2();
            }
        });
    }

    private void setImage() {
        checkBox.setFocusTraversable(false);
        Image image = new Image(file.toURI().toString());
        ImageView iv = new ImageView(image);
        iv.autosize();
        iv.setFitWidth(100);
        iv.setFitHeight(100);
        iv.setPreserveRatio(true);
        iv.setSmooth(true);
        iv.setCache(true);
        setAlignment(Pos.BOTTOM_CENTER);
        setMaxSize(100,130);
        Label  label =new Label(file.getName());
        getChildren().addAll(iv,label ,checkBox);
        setFileTips();
    }

    private void initContextMenu() {

        if(!ImageBoxes.contextMenu.getItems().isEmpty()){
            ImageBoxes.contextMenu.getItems().clear();
        }
        ImageBoxes.contextMenu.setAutoHide(true);
        MenuItem item1 =new MenuItem("打开");
        MenuItem item2 =new MenuItem("复制");
        MenuItem item3 =new MenuItem("剪切");
        MenuItem item4 =new MenuItem("删除");
        MenuItem item5 =new MenuItem("重命名");
        item1.setOnAction(event -> {
            ImageBoxes.setCurrentImageFile(file);
            openAction();
        });
        item2.setOnAction(event -> {
            mainController.setFlag_Cut(false);
            ImageBoxes.clearCutImageBoxes();
            ImageBoxes.getCopiedFile().clear();
            if(ImageBoxes.getSelectedImageBoxes().size()==0){
                ImageBoxes.getSelectedImageBoxes().add(this);
            }
            ImageBoxes.copySelectedImageBoxes();
        });
        item3.setOnAction(event -> {
            mainController.setFlag_Cut(true);
            ImageBoxes.clearCutImageBoxes();
            if(ImageBoxes.getSelectedImageBoxes().size()==0)
                this.getCheckBox().setSelected(true);
            ImageBoxes.cutImageBoxes();
        });
        item4.setOnAction(event -> {
            if(MyAlert.showAlert1("是否删除选中的图片？", "", Main.getMainStage()))
            {
                System.out.println(ImageBoxes.getSelectedImageBoxes().size());
                if(ImageBoxes.getSelectedImageBoxes().size()==0){
                    this.delete();
                    MainController.getMainController().Refresh();
                }
                ImageBoxes.deleteSelectedImageBoxes();
            }
            else {
                ImageBoxes.uncheck();
            }

        });
        item5.setOnAction(event -> {

            if(ImageBoxes.getSelectedImageBoxes().size()==0)
            {
                this.getCheckBox().setSelected(true);
                single=true;
            }
            else single= ImageBoxes.getSelectedImageBoxes().size() == 1;
            ImageBoxes.renameImageBoxes(single);
        });
        ImageBoxes.contextMenu.getItems().addAll(item1,item2,item3,item4,item5);
    }
    public File getFile() {
        return file;
    }
    public void delete(){
        System.out.println(file);
        if(file.delete()){
            System.out.println("删除成功");
        }
        else System.out.println("删除失败");
        //MainController.getMainController().Refresh();
    }

    public void initEvent(){
        /*
         * 鼠标事件初始化
         */
        setOnMouseEntered(event -> {
            if (!checkBox.isSelected()){
                imageBox.setStyle("-fx-background-color: rgba(0, 0, 0, 0.07)");
            }
        });
        setOnMouseExited(event ->
        {
            if(!checkBox.isSelected()) {
                setStyle("-fx-background-color: transparent");
            }
        });

        setOnMouseClicked(event -> {


            if(event.isControlDown()&&event.getButton().equals(MouseButton.PRIMARY)){
                checkBox.setSelected(!checkBox.isSelected());
            }
            else if(event.getClickCount()==2&&event.getButton().equals(MouseButton.PRIMARY)){
                openAction();
            }else
            if(event.getButton().equals(MouseButton.PRIMARY)){
                {
                    ImageBoxes.uncheck();
                    checkBox.setSelected(true);
                }
            }
            event.consume();
        });


    }

    private  void setFileTips(){
        long fileLength =file.length();
        String tips = String.format("%d b",fileLength);
        int kb = 1024;
        String toolTip;
        tips = MainController.getImagesLength(fileLength, tips, kb);
        toolTip = String.format("名称: %s\n大小: %s", file.getName(), tips);
        Tooltip tooltip =new Tooltip(toolTip);
        Tooltip.install(this,tooltip);

    }
    public CheckBox getCheckBox(){
        return this.checkBox;
    }
    private void openAction() {
        System.out.println(file);
        ImageBoxes.setCurrentImageFile(file);
        Main.getMainStage().hide();
        Stage stage = new Stage();
        ImageBoxes.setFunctionUIStage(stage);

        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("FunctionUI.fxml"));
        stage.setTitle("图片展示");
        stage.getIcons().add(new Image("file:src/main/resources/icon/图片.png"));
        Scene scene = null;
        try {
            scene = new Scene(fxmlLoader.load());
        } catch (IOException e) {
            e.printStackTrace();
        }
        stage.setScene(scene);
        stage.show();
        stage.setOnCloseRequest(event -> Main.getMainStage().show());
    }
}
